package com.niiwoo.civet.account.service.local.refund.newRefund;

import com.alibaba.fastjson.JSONObject;
import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.niiwoo.civet.account.dao.entity.AccountBase;
import com.niiwoo.civet.account.dao.entity.BatchTransOrder;
import com.niiwoo.civet.account.dao.entity.ProTransactionOrder;
import com.niiwoo.civet.account.dao.mapper.AccountBaseMapperExt;
import com.niiwoo.civet.account.dao.mapper.BatchTransOrderDetailBizMapperExt;
import com.niiwoo.civet.account.dao.mapper.BatchTransOrderMapperExt;
import com.niiwoo.civet.account.dao.mapper.ProTransactionOrderMapperExt;
import com.niiwoo.civet.account.dto.common.AccountBatchTransOrderDTO;
import com.niiwoo.civet.account.enums.CanRefundEnum;
import com.niiwoo.tripod.lanmao.enums.TransactionTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * Created by dell on 2018/1/31.
 * 还款查询服务
 */
@Service
@Slf4j
public class RefundQueryLocalService {
    @Autowired
    private AccountBaseMapperExt accountBaseMapperExt;
    @Autowired
    private BatchTransOrderMapperExt batchTransOrderMapperExt;
    @Autowired
    private BatchTransOrderDetailBizMapperExt batchTransOrderDetailBizMapper;
    @Autowired
    private ProTransactionOrderMapperExt proTransactionOrderMapperExt;

//    public AccountBase getAccountBase(AccountBatchTransOrderDTO dto) {
//        UserRoleEnum userRoleEnum ;
//        String userId;
//        if (TransactionTypeEnum.COMPENSATORY.getValue().equals(dto.getTradeType())) {
//            userRoleEnum = UserRoleEnum.GUARANTEECORP;
//            userId = dto.getGuaranteeUserId();
//        }else{
//            userRoleEnum = UserRoleEnum.BORROWERS;
//            userId = dto.getBorrowerUserId();
//        }
//        if (!HintManagerHolder.isMasterRouteOnly()) {
//            HintManagerHolder.clear();
//            HintManager hintManager = HintManager.getInstance();
//            hintManager.setMasterRouteOnly();
//        }
//        AccountBase accountBase = accountBaseMapperExt.selectByUserIdAndAccountRole(userId, userRoleEnum.getValue());
//        return accountBase;
//    }

    /**
     * 更新还款订单状态
     *
     * @param batchNo 批次请求号
     * @return
     */

    public Byte queryBatchNoStatus(String batchNo) {
        if (!HintManagerHolder.isMasterRouteOnly()) {
            HintManagerHolder.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.setMasterRouteOnly();
        }
        BatchTransOrder batchTransOrder = batchTransOrderMapperExt.selectOrderByBatchRequestNo(batchNo);
        if (batchTransOrder != null) {
            return batchTransOrder.getStatus();
        }
        log.error("account batchTransOrder数据为空,batchNo={}",batchNo);
        return (byte)0;
    }

    public CanRefundEnum checkProjectCanRefund(Long projectId, Integer currentPeriod, Byte type) {
        int exist = 0;
        //立即还款之后可以进行提前还清，因此控制只判断当前是否有正在还款中的
//        if (type == null || RepayStatusEnum.NORMAL.getCode() == type) {
//            exist = batchTransOrderMapperExt.queryOrderByProIdAndPeriod(projectId, currentPeriod);
//        } else {
        exist = batchTransOrderMapperExt.queryOverdueOrderByProIdAndPeriod(projectId);
//        }

        return exist == 0 ? CanRefundEnum.CAN_REFUND : CanRefundEnum.REFUNDING;
    }

    @Transactional
    public boolean checkRepaymentMoney(String batchRequestNo){
        BigDecimal totalAmountBiz =batchTransOrderDetailBizMapper.totalAmountByBatchNo(batchRequestNo);
        if (totalAmountBiz == null){
            log.error("批次号{}明细金额数据为统计为空",batchRequestNo);
            return false;
        }
        ProTransactionOrder proTransactionOrder = proTransactionOrderMapperExt.queryProTransactionOrderByBatchRequestNo(batchRequestNo);
        log.info("银行存管batchRequestNo={},totalAmountBiz={},proTransactionOrder={}",batchRequestNo,totalAmountBiz, JSONObject.toJSONString(proTransactionOrder));
        if (proTransactionOrder!=null && proTransactionOrder.getAmount().compareTo(totalAmountBiz)!=0){
            log.info("分账额度与冻结资金不等，直接中止,不请求存管还款接口,batchNo={}",batchRequestNo);
            return false;
        }
        return true;
    }

}
